package com.ssyt.yt1.day22;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

class Person implements Serializable {

	/**
	 * <pre>
	 * Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本
	 * 一致性的。在做反序列化时，JVM会把传来的字节流中的serialVersionUID
	 * 与本地相应实体（类）的serialVersionUID进行比较，如果相同就认为是一 致的，可以进行反序列化，否则就会出现序列化版本不一致的异常。
	 * 
	 * <pre/>
	 */
	private static final long serialVersionUID = 1467417608707280842L;

	private transient int id;
	private String name;
	private int age;

	public Person() {
		super();
	}

	public Person(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
	}

}

/**
 * <pre>
 * 对象序列化 
 * 1、序列化就是把一个对象变为二进制的数据流的一种方法，通过对象序列化可 以方便的实现对象的传输或存储。
 * 2、什么情况下需要序列化？
 *  1）当你想把的内存中的对象状态保存到一个文件中或者数据库中时候；
 * 	2）当你想用套接字在网络上传送对象的时候；
 * 	3）当你想通过RMI传输对象的时候；
 * </pre>
 * 
 * @author Administrator
 *
 */
public class SerializableDemo {

	/**
	 * 序列化对象：把对象存到硬盘上
	 * 
	 * @throws IOException
	 */
	public static void test1() throws IOException {
		Person person = new Person(123456, "张三", 18);

		File file = new File("d:/object.txt");
		if (!file.exists()) {
			file.createNewFile();
		}
		OutputStream outputStream = null;
		ObjectOutputStream objectOutputStream = null;
		try {
			outputStream = new FileOutputStream(file);
			objectOutputStream = new ObjectOutputStream(outputStream);
			objectOutputStream.writeObject(person);
		} finally {
			if (objectOutputStream != null) {
				objectOutputStream.close();
			}
			if (outputStream != null) {
				outputStream.close();
			}
		}
		System.out.println("序列化成功！");
	}

	/**
	 * 反序列化
	 * 
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public static void test2() throws IOException, ClassNotFoundException {
		File file = new File("d:/object.txt");
		if (!file.exists()) {
			file.createNewFile();
		}
		InputStream inputStream = null;
		ObjectInputStream objectInputStream = null;
		try {
			inputStream = new FileInputStream(file);
			objectInputStream = new ObjectInputStream(inputStream);
			Object obj = objectInputStream.readObject();
			if (obj instanceof Person) {
				Person person = (Person) obj;
				System.out.println(person);
			}
		} finally {
			if (objectInputStream != null) {
				objectInputStream.close();
			}
			if (inputStream != null) {
				inputStream.close();
			}
		}
		System.out.println("序列化成功！");
	}

	public static void main(String[] args) throws IOException,
			ClassNotFoundException {
		// test1();
		test2();
	}
}
